package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.BatteryCheckReq;
import com.rightsidetech.system.entity.SysBatteryCheck;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysBatteryCheckService;
import com.rightsidetech.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.admin.system
 * @ClassName: SysBatteryCheckController
 * @Author: yinj
 * @CreateTime: 2022/01/05 16:18
 * @Description: 电池检测相关
 */
@RestController
@RequestMapping("/admin/battery_check")
@ShenyuSpringMvcClient(path = "/admin/battery_check/**")
@Api(tags = "admin:电池检测相关")
public class SysBatteryCheckController extends BaseController {

    @Resource
    private SysBatteryCheckService sysBatteryCheckService;
    @Resource
    private SysUserService sysUserService;


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("电池检测列表")
    public ResponseData<PageInfo<SysBatteryCheck>> page(BatteryCheckReq request) throws Exception {

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long loginId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginId);
        if (ObjectUtils.isEmpty(loginInfo)) {
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        request.setAreaId(loginInfo.getSysCompanyAreaId());

        return sysBatteryCheckService.page(request);
    }


}
